﻿namespace td.Core.Xml
{
    using System;
    using System.IO;
    using System.Reflection;
    using System.Xml;
    using System.Windows.Forms;

    public class RuntimeXml : IXmlStorage
    {
        private string assemblyName;
        private string hintPath;
        private bool isAssemblyLoaded;
        private Assembly loadedAssembly;
        private string name;

        public RuntimeXml()
        {
            this.loadedAssembly = null;
            this.hintPath = "";
            this.assemblyName = "";
            this.name = "";
        }

        public RuntimeXml(string assembly, string hintPath)
        {
            this.loadedAssembly = null;
            this.assemblyName = assembly;
            this.hintPath = hintPath;
        }

        public object CreateInstance(string instance)
        {
            Assembly loadedAssembly = this.LoadedAssembly;
            if (loadedAssembly == null)
            {
                return null;
            }
            return loadedAssembly.CreateInstance(instance);
        }

        public void GetValueFromXml(XmlNode objectNode)
        {
            if ((objectNode != null) && objectNode.Name.ToLower().Equals("runtime"))
            {
                XmlAttributeCollection attributes = objectNode.Attributes;
                for (int i = 0; i < attributes.Count; i++)
                {
                    XmlAttribute attribute = attributes[i];
                    if (attribute.Name.ToLower().Equals("hintpath"))
                    {
                        this.hintPath = attribute.Value;
                    }
                    if (attribute.Name.ToLower().Equals("assembly"))
                    {
                        this.assemblyName = attribute.Value;
                    }
                    if (attribute.Name.ToLower().Equals("name"))
                    {
                        this.name = attribute.Value;
                    }
                }
            }
        }

        public void SetValueToXml(XmlDocument xmldoc, XmlElement parentEle)
        {
            XmlElement newChild = xmldoc.CreateElement("Runtime");
            newChild.SetAttribute("hintpath", this.hintPath);
            newChild.SetAttribute("name", this.name);
            newChild.SetAttribute("assembly", this.assemblyName);
            parentEle.AppendChild(newChild);
        }

        public override string ToString()
        {
            return ("[RuntimeXml: " + this.name + "]");
        }

        public string AssemblyName
        {
            get
            {
                return this.assemblyName;
            }
            set
            {
                this.assemblyName = value;
            }
        }

        public string HintPath
        {
            get
            {
                return this.hintPath;
            }
            set
            {
                this.hintPath = value;
            }
        }

        public Assembly LoadedAssembly
        {
            get
            {
                if (!this.isAssemblyLoaded)
                {
                   // LoggingService.Info("开始加载运行库: " + this.assemblyName);
                    this.isAssemblyLoaded = true;
                    try
                    {
                        string path = Path.Combine(AppDomain.CurrentDomain.SetupInformation.ApplicationBase, this.assemblyName.Trim());
                        this.loadedAssembly = Assembly.LoadFile(path);
                        //if (this.loadedAssembly == null)
                        //{
                        //    string path = Path.Combine(AppDomain.CurrentDomain.SetupInformation.ApplicationBase, this.hintPath);
                        //    if (File.Exists(path))
                        //    {
                        //        this.loadedAssembly = Assembly.LoadFile(path);
                        //    }
                        //}
                        this.loadedAssembly.GetExportedTypes();
                    }
                    catch (FileNotFoundException exception)
                    {
                        MessageBox.Show(exception.ToString());
                       // LoggingService.Info("运行库[" + this.assemblyName + "]加载失败:\n" + exception.ToString());
                    }
                    catch (FileLoadException exception2)
                    {
                        //LoggingService.Info("运行库[" + this.assemblyName + "]加载失败:\n" + exception2.ToString());
                    }
                }
                return this.loadedAssembly;
            }
        }

        public string Name
        {
            get
            {
                return this.name;
            }
            set
            {
                this.name = value;
            }
        }
    }
}

